const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: defaultAvatarUrl,
    upLoadImg: '',
    nickname: '',
    FjLoginLog: '',
    openInfo: {},
    isEdit: false
  },
  // 选择头像
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl,
    })
  },
  // 提交
  bindblur(e){
    this.setData({
      nickname: e.detail.value
    })
  },
  bindsubmit(e){
    if(this.data.avatarUrl === defaultAvatarUrl){
      return wx.showToast({
        title: '请上传头像',
        icon: 'none'
      })
    } else if (!this.data.nickname){
      return wx.showToast({
        title: '请输入昵称',
        icon: 'none'
      })
    }
    if(this.data.avatarUrl === this.data.upLoadImg){
      this.submitRequest()
    } else {
      this.upLoadImgApi(this.data.avatarUrl)
    }
  },

  // 上传头像接口
  upLoadImgApi(filePath) {
    wx.showLoading({
      title: '',
    })
    let that = this
    wx.uploadFile({
      url: app.data.api_url + 'api/FeijiuApp/GqImageUpload?strRSA=' + app.data.strRSA,
      filePath: filePath,
      name: 'file',
      success: function (info) {
        wx.hideLoading()
        that.setData({
          upLoadImg: JSON.parse(info.data)[0]
        })
        that.submitRequest()
      }
    })
  },
  submitRequest(){
    const url = this.data.isEdit 
      ? "api/FeijiuApp/Feijiu_HeZuo_WxUserUpdate"
      : "api/FeijiuApp/Feijiu_HeZuo_WxUserADD"
    const params = {
      headpic: this.data.upLoadImg,
      nickname: this.data.nickname,
      LoginUID: this.data.FjLoginLog.UID,
      openid: this.data.openInfo.openid
    }
    app.getDaoPostN(url, params, function(r){
      if(r.code == 200){
        wx.showToast({
          title: '添加成功',
          icon: "none"
        })
        setTimeout(()=>{
          wx.navigateBack()
        }, 500)
      }else{
        wx.showToast({
          title: r.message,
          icon:'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    const _this = this
    this.getOpenId()
    const eventChannel = this.getOpenerEventChannel()
    if(Object.keys(eventChannel).length === 0) return
    eventChannel.on('accountInfo', function(data) {
      _this.setData({
        avatarUrl: data.data.headpic,
        upLoadImg: data.data.headpic,
        nickname: data.data.nickname,
        isEdit: true
      })
    })
  },
  onShow() {
    this.setData({
      FjLoginLog : wx.getStorageSync('FjLoginLog')
    })
  },
  // 客户端无法从缓存中拿到openid
  getOpenId(){
    const _this = this
    wx.login({
      success: (res) => {
        if (res.code) {
          var api_input = {
            code: res.code
          }
          app.getDaoPostN('api/FeijiuApp/Feijiu_XCX_GetCode', api_input, function (r) {
            if (r.code == 200) {
              _this.setData({
                openInfo: JSON.parse(r.data)
              })
            } else {
              wx.showToast({
                title: '网络延迟较高，请检测网络后再试。',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  }
})
